#include <iostream>
#include <memory>
#include <vector>
#include <map>

#include <yaml-cpp/yaml.h>

#include "../include/circular_list.hpp"

int main()
{
    YAML::Node data_file;
    try
    {
        data_file = YAML::LoadFile("../data/Data.yaml");
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }

    std::vector<int> test = data_file["Joseph"]["data"].as<std::vector<int>>();
    my_data::CircularList clist;

    std::map<int,int> map;
    for(int i = 0;i<test.size();++i)
    {
        int key = test[i];
        clist.push_back(i+1);
        map.insert({i+1,key});
    }
    // 初始位置m
    int m = data_file["Joseph"]["m"].as<int>();
    // 当前位置idx
    int idx = 0;
    // 约瑟夫环模拟，一直按顺序弹出到循环链表空即可
    while(!clist.empty())
    {
        // 求弹出的位置
        int position = (idx + m)%clist.length - 1;
        // 如果弹出的位置是队尾，position将会计算为-1
        if(position < 0)
        {
            position = clist.length-1;
        }
        int id = clist.remove(position);
        m = map[id];

        idx = position;
        std::cout<<id<<std::endl;
    }


}